<template>
  <div class="content">
    <div class="common-seach-wrap" :style="{ marginBottom: '10px' }">
      <el-button
        type="primary"
        icon="Plus"
        @click="handleOpen()"
        v-auth="'carfilm/add'"
        >添加产品</el-button
      >
    </div>
    <el-table :data="tableData" row-key="id" height="100%" stripe>
      <el-table-column type="index" label="#" fixed="left" />
      <el-table-column prop="id" label="ID" fixed="left" />
      <el-table-column prop="part_name" label="产品名称" fixed="left" />
      <el-table-column prop="category" label="所属类目" />
      <el-table-column prop="price" sortable label="施工价格" />
      <el-table-column prop="period" sortable label="质保年限" />
      <el-table-column prop="status" label="状态">
        <template #default="scope">
          <el-switch
            v-model="scope.row.status"
            active-text="显示"
            inactive-text="不显示"
            @change="setStatus($event, scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="200">
        <template #default="scope">
          <el-button
            type="primary"
            link
            icon="Edit"
            @click="handleOpen(scope.row.id)"
            v-auth="'carfilm/edit'"
          >
            编辑
          </el-button>
          <el-button
            type="danger"
            link
            icon="DeleteFilled"
            @click="delInfo(scope.row.id)"
            v-auth="'carfilm/del'"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-divider border-style="none" />
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      background
      :current-page="curPage"
      :page-size="pageSize"
      layout="total, sizes,prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <Drawer
      ref="examineDraw"
      @getTable="getTableInfo"
      :category="categoryOption"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from "vue";
import Drawer from "./component/dialogForm.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { categoryOptionsApi, delApi, editApi, getListAPi } from "@/api/carfilm";
const examineDraw = ref<any>(null);
const query = reactive({
  class_id: "0",
});
const total = ref(0);
const curPage = ref(1);
const pageSize = ref(20);
const categoryOption = ref<any[]>([]);
const tableData = ref([]);
onMounted(() => {
  categoryOptionsApi().then((res: any) => {
    categoryOption.value = res.data;
  });
  getTableInfo();
});
const handleSizeChange = (val: number) => {
  pageSize.value = val;
  getTableInfo();
};
const handleCurrentChange = (val: number) => {
  curPage.value = val;
  getTableInfo();
};
function getTableInfo() {
  getListAPi({
    page: curPage.value,
    page_size: pageSize.value,
    ...query,
  }).then((res: any) => {
    tableData.value = res.data.data;
    total.value = res.data.total;
  });
}

function delInfo(id: number) {
  ElMessageBox.confirm("确认要删除此信息吗?", "警告！", {
    confirmButtonText: "确认删除",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delApi(id).then((res: any) => {
        getTableInfo();

        ElMessage({
          type: "success",
          message: "删除成功！",
        });
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "删除已取消",
      });
    });
}

// 打开添加数据窗口
const emits = defineEmits(["openForm", "search"]);
function handleOpen(id?: number) {
  examineDraw.value.dialogOpen(id || 0);
}
const setStatus = (status: boolean, row: any) => {
  row.status = status ? 1 : 0;
  editApi(row).then((res: any) => {
    ElMessage({
      type: "success",
      message: "修改成功！",
    });
  });
};
</script>

<style lang="scss">
.el-tabs {
  .el-tabs__new-tab {
    width: auto !important;
  }
}
.table-box {
  height: calc(100% - 200px);
}
</style>
